//
// Created by grainlee on 16/9/8.
//
#include <iostream>
using namespace std;

int myAtoi(string str) {
    int max = 0x7fffffff;
    int min = 0x80000000;

    long long int res =0 ;
    int isNeg =0;
    int i =0 ;

    while (str[i] == ' ' && i < str.length())
        i++;
    if (i == str.length())
        return res;

    if (str[i] == '-'){
        isNeg = 1;
        i++;
    }else if(str[i] == '+'){
        i++;
    }
    for (; i < str.length();i++ ){
        if (str[i] <= '9' && str[i] >= '0'){
            if (isNeg){
                res = res*10 - (str[i] - '0');
                if (res < min)
                    return min;
            }else{
                res = res*10 + str[i] - '0';
                if (res > max)
                    return max;
            }
        }else
            break;

    }
    return (int)res;
}
int main() {
    string s = "2147483648";
    cout << myAtoi(s);
}
